package com.andnnl.map;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.andnnl.future.ThreadUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by chenss on 2023/11/3.
 */
public class MapRemoveTest {
    public static void main(String[] args) throws IOException {
        //判断删除不存在的网关
//        Map<Integer, Integer> lastRatioMap = new HashMap<>();//会死锁
        Map<Integer, Integer> lastRatioMap = new ConcurrentHashMap<>();//安全
        for (int i = 0; i < 1000000; i++) {
            lastRatioMap.put(i, i);
        }
//        lastRatioMap.put("asdf2","asdf2");
//        lastRatioMap.put("asdf3","asdf3");

        ExecutorService pool = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            int finalI = i;
            pool.submit(() -> {
                Iterator<Integer> iterator = lastRatioMap.keySet().iterator();
                while (iterator.hasNext()) {
                    Integer ogw = iterator.next();
                    if (ogw % 10 == finalI) {
                        iterator.remove();
                        System.out.println("remove "+ogw);
//                logger.info("省网分流 key:"+newKey+", 删除不存在的gw:"+ogw);
                    }
                }
            });
        }
//        pool.shutdown();
//        System.in.read();
//        logger.info("lastBean.lastRatioMap剔除:"+JsonLib.toJson(lastBean.lastRatioMap));

        while (true) {
            System.out.println("lastRatioMap:" + lastRatioMap.size());
            ThreadUtil.sleep(2000);
        }
    }
}
